package cn.example.web.plugins.storage;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import org.artifact.core.db.BaseDao;
import org.artifact.core.lang.IServer;
import org.artifact.core.plugin.storage.StoragePlugin;

import java.util.HashSet;
import java.util.Set;

public class StoragePluginFactory {

	public com.jfinal.plugin.IPlugin create() {
		StoragePlugin storagePlugin = new StoragePlugin("0/1 * * * * ?",()->{
			Set<BaseDao<?>> daos = new HashSet<>();
			Set<Class<?>> clazzs = ClassUtil.scanPackageBySuper("cn.example.web", BaseDao.class);
			for (Class<?> clazz : clazzs) {
				if(ClassUtil.isNormalClass(clazz)) {
					String className = clazz.getSimpleName();
					String key = StrUtil.lowerFirst(className);
					BaseDao<?> baseDao = IServer.me().getContext().getSpringContext().getBean(key,BaseDao.class);
					daos.add(baseDao);
				}
			}
			return daos;
		});
		return new com.jfinal.plugin.IPlugin() {
			@Override
			public boolean stop() {
				return storagePlugin.stop();
			}
			
			@Override
			public boolean start() {
				return storagePlugin.start();
			}
		};
	}

}
